/*
 * @author Javier A. Ramírez M.
 * jaramirez85@yahoo.es
 */
package com.jav;

import com.jav.bean.Producto;
import com.jav.inter.Constantes;
import com.jav.per.PersisProducto;
import com.jav.per.Persistencia;
import com.sun.lwuit.Button;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.util.Resources;
import java.util.Hashtable;
import javax.microedition.rms.RecordEnumeration;

public class ProveedorContainer extends Container implements Constantes {

    Button bt_buscar;
    private TextField search = new TextField("");
    private Label lb_vendido = new Label("");
    private Label lb_cambio = new Label("");

    public ProveedorContainer() {
        Resources res = null;
        try {
            
            res = Datos.getInstance().getRes();
            setLayout(new BoxLayout(BoxLayout.Y_AXIS));
            search.setConstraint(TextField.NUMERIC);
            search.setInputMode("123");
            bt_buscar = new Button(res.getImage("search.png"));
            bt_buscar.setAlignment(Button.CENTER);
            bt_buscar.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent arg0) {
                    cargaDatos();
                }

                private void cargaDatos() {
                    if (search.getText() == null) {
                        return;
                    }
                    if (search.getText().trim().length() == 0) {
                        return;
                    }
                    Persistencia p = null;
                    long sumVendido = 0;
                    long sumCambios = 0;

                    Hashtable hm = new Hashtable();

                    try {

                        if (Datos.getInstance().getProductos() != null) {

                            int idProvIn = Integer.parseInt(search.getText());
                            p = new Persistencia(PERS_DETALLE);
                            RecordEnumeration re = p.getRecordStore().enumerateRecords(null, null, false);
                            while (re.hasNextElement()) {
                                PersisProducto pp = new PersisProducto();
                                pp.resurrect(re.nextRecord());
                                int idProvOut = Integer.parseInt(pp.getProveedor());
                                if (idProvIn == idProvOut) {
                                    long precio = getPrecioProd(pp.getProd(), idProvIn);
                                    sumVendido += pp.getCantidad() * precio;
                                    sumCambios += pp.getCambios() * precio;
                                }
                            }
                            re = null;
                            lb_cambio.setText(String.valueOf(sumCambios));
                            lb_vendido.setText(String.valueOf(sumVendido));

                        } else {
                            lb_cambio.setText("INFO PRODUCTOS");
                            lb_vendido.setText("DEBE CARGAR");
                        }


                    } catch (Exception ex) {
                        ex.printStackTrace();
                        lb_cambio.setText("ERROR");
                        lb_vendido.setText("ERROR");
                    } finally {
                        try {
                            if (p != null) {
                                p.close();
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }

                private long getPrecioProd(String idProd, int prov) {

                    Hashtable productos = Datos.getInstance().getProductos();
                    Producto p = (Producto) productos.get(idProd);
                    long precio = 0;
                    if (p != null && prov == Integer.parseInt(p.getProv())) {
                        precio = Long.parseLong(p.getPrecio());
                    }
                    productos= null;
                    p = null;
                    return precio;
                }
            });

            Container c1 = new Container(new BorderLayout());

            Dimension d = bt_buscar.getPreferredSize();
            d.setWidth(Math.max(d.getWidth(), 40));
            bt_buscar.setPreferredSize(d);
            c1.addComponent(BorderLayout.CENTER, search);
            c1.addComponent(BorderLayout.EAST, bt_buscar);
            addComponent(c1);
            Container largest = createPair("Ventas: $", lb_vendido, 30);
            int largestW = largest.getComponentAt(0).getPreferredW();
            addComponent(largest);
            largest = createPair("Cambio: $", lb_cambio, largestW);
            addComponent(largest);


        } catch (Exception e) {
            Datos.getInstance().addLogs(e.getMessage());
        } finally {
            res = null;
        }

    }

    protected Container createPair(String label, Component c, int minWidth) {
        Container pair = new Container(new BorderLayout());
        Label l = new Label(label);
        Dimension d = l.getPreferredSize();
        d.setWidth(Math.max(d.getWidth(), minWidth));
        l.setPreferredSize(d);
        //l.getStyle().setBgTransparency(100);
        pair.addComponent(BorderLayout.WEST, l);
        pair.addComponent(BorderLayout.CENTER, c);
        return pair;
    }
}
